//
//  RepairOrderDetailViewController.swift
//  PropertyCommunity
//
//  Created by benwang on 2021/6/27.
//

import UIKit
import SnapKit
import RxSwift

class RepairOrderDetailViewController: UIViewController {

  enum OrderContentType: Int {
    case master  = 0
    case holder  = 1
    case area    = 2

    var tag: String { "\(self.hashValue)"}
  }

  private var handler = PTSheetHandle()
  private let disposeBag = DisposeBag()
  var datasource: [OrderContentType] = [.master, .holder, .area]
  var type: RepairOrderType = .waitingRepair

  override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    setupTreeView()
  }

  private lazy var tableView: UITableView = {
    let tb = UITableView(frame: .zero, style: .plain)
    tb.delegate = self
    tb.dataSource = self
    tb.separatorStyle = .none
    tb.register(RepairOrderMasterCell.self, forCellReuseIdentifier: OrderContentType.master.tag)
    tb.register(RepairOrderHolderCell.self, forCellReuseIdentifier: OrderContentType.holder.tag)
    tb.register(RepairOrderAreaCell.self, forCellReuseIdentifier: OrderContentType.area.tag)
    tb.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 88))
    return tb
  }()

  /*
   // MARK: - Navigation

   // In a storyboard-based application, you will often want to do a little preparation before navigation
   override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
   // Get the new view controller using segue.destination.
   // Pass the selected object to the new view controller.
   }
   */

}

extension RepairOrderDetailViewController {

  func setupTreeView() {

    title = type.rawValue

    opacityNavigationBar(color: .white)

    view.backgroundColor = UIColor(hex: 0xFAFAFA)

    view.addSubview(tableView)
    tableView.snp.makeConstraints { make in
      make.top.equalTo(view.safeAreaLayoutGuide.snp.top)
      make.left.right.equalToSuperview()
      make.bottom.equalToSuperview()
    }

    switch type {
    case .waitingRepair:
      waitingRepairContent()
    case .repaired:
      repairedContent()
    case .transcription:
      transcriptionContent()
    default:
      ()
    }
  }

  // 待维修
  func waitingRepairContent() {

    let bottomView = R.nib.repariOrderDetailBottom(owner: self)!

    view.addSubview(bottomView)
    bottomView.snp.makeConstraints({ make in
      make.left.right.bottom.equalToSuperview()
      make.height.equalTo(88)
    })

    bottomView.completeTap.subscribe(onNext: {
      self.handler.sheet(completeRepair: nil) { index in

      }
    })
    .disposed(by: disposeBag)
  }

  // 已维修
  func repairedContent() {
    let button = UIButton(type: .custom)
    button.setTitle("查看评价", for: .normal)
    button.setTitleColor(.designKit.primaryText, for: .normal)
    button.setBackgroundImage(.image(with: .designKit.primary), for: .normal)
    button.titleLabel?.font = .designKit.body
    button.cornerRadius = 8
    button.clipsToBounds = true
    view.addSubview(button)

    button.snp.makeConstraints({ make in
      make.left.equalToSuperview().offset(16)
      make.right.equalToSuperview().offset(-16)
      make.height.equalTo(48)
      make.bottom.equalToSuperview().offset(-40)
    })

    button.rx.tap.subscribe(onNext: {
      self.handler.sheet(repairEvaluation: nil) { index in

      }
    })
    .disposed(by: disposeBag)
  }

  // 已转单
  func transcriptionContent() {
    waitingRepairContent()
  }
}

extension RepairOrderDetailViewController: UITableViewDelegate, UITableViewDataSource {

  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return datasource.count
  }

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let type = datasource[indexPath.row]
    let cell = tableView.dequeueReusableCell(withIdentifier: type.tag, for: indexPath)
    if let item = cell as? RepairOrderMasterCell  {

      item.transferOrderAction.subscribe(onNext: {

        self.handler.sheet(repairTrans: nil) { index in

        }

      })
      .disposed(by: item.disposeBag)
    }

    return cell
  }
}
